package com.fengpanlong.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.fengpanlong.entity.Message;
import com.fengpanlong.util.DBHelp;
import com.fengpanlong.util.RowMapper;

public class MessageDao {
	private DBHelp<Message> db = new DBHelp<Message>();
	
	private class MessageRowMapper implements RowMapper<Message> {

		public Message mapperRow(ResultSet rs) throws SQLException {
			Message m = new Message();
			m.setId(rs.getString("id"));
			m.setContent(rs.getString("content"));
			m.setCreatetime(rs.getString("createtime"));
			m.setEmployeeId(rs.getString("employeeId"));
			m.setProjectId(rs.getString("projectId"));
			return m;
		}
		
	}
	/**
	 * 保存消息
	 * @param message 消息对象
	 */
	public void save(Message message) {
		String sql = "insert into t_message(id,content,createtime,employeeid,projectid) values(?,?,?,?,?)";
		db.executeSQL(sql, message.getId(),message.getContent(),message.getCreatetime(),message.getEmployeeId(),message.getProjectId());
	}
	/**
	 * 通过项目id查找信息列表
	 * @param projectId 项目id
	 * @return 信息列表
	 */
	public List<Message> findMessagesByProjectId(String projectId) {
		String sql = "select id,content,createtime,employeeid,projectid from t_message where projectid = ? order by createtime desc";
		return db.executeQueryForList(sql, new MessageRowMapper(), projectId);
	}
	/**
	 * 获得增加的消息
	 * @param num
	 * @return
	 */
	public List<Message> getAddMsg(int num) {
		int count = getMsgCount();
		int a = count - num;
		String sql = "select id,content,createtime,employeeid,projectid from t_message order by createtime desc limit 0,?";
		return db.executeQueryForList(sql, new MessageRowMapper(), a);
	}
	/**
	 * 查找消息总数
	 * @return 项目总数
	 */
	private int getMsgCount() {
		String sql = "select count(*) from t_message";
		return db.executeQueryForCount(sql);
	}
}
